﻿/*==============================================================
 * ArticleCateExtensions.cs
 * Copyright (C) 2014 贵州惠众互联 Inc. All rights reserved.
 *==============================================================
 * Author:   罗应红
 * Date:     2014/03/03 15:18:26.
 * Version:  1.0
 * QQ:		 88962800
*==============================================================*/

using System.Collections.Generic;
using System.Linq;
using QDT.Core.Domain;
using QDT.Web.Admin.Models;

namespace QDT.Web.Admin.Helper {
    public static  class ArticleCateExtensions {

        public static IList<ArticleCateModel> ToModelList(this IList<ArticleCate> cates ) {
            var reuslt = new List<ArticleCateModel>();
            ToModel(reuslt,cates);
            return reuslt;
        }
        private static void ToModel(IList<ArticleCateModel> container,IEnumerable<ArticleCate> cates ) {
            foreach(var c in cates) {
                var m = new ArticleCateModel {
                    Brief = c.Brief,
                    Id = c.ID,
                    Title = c.Title,
                    IsEffect = c.IsEffect,
                    IsDelete = c.IsDelete,
                    Sort =  c.Sort,
                    ParentId = c.ParentId,
                    TypeId = c.InnerArticleTypeId
                };
                container.Add(m);
                if(c.Child.Any()) {
                    ToModel(m.Child, c.Child);
                }
            }
        }

        public static List<ArticleCateTree> ToTreeNode(this IList<ArticleCateModel> source ) {
             if(source == null || !source.Any()) {
                 return new List<ArticleCateTree>();
             }
             var result = new List<ArticleCateTree>();
             ToFormatTree(result,source);
             return result;
              
         }
        private static string StrignRepat(string orginal,int level) {
            if(level == 0) {
                return string.Empty;
            }
            var tmp = orginal;
            for (int i = 0; i <= level-1; i++) {
                tmp = string.Concat(tmp, orginal);
            }
            return tmp;
        }

        private static  void ToFormatTree(List<ArticleCateTree> container,IEnumerable<ArticleCateModel> models,int leve = 0) {
            const string split = "|--";
            foreach (var m in models) {
                if(m.IsDelete) {
                    continue;
                }
                var tree = new ArticleCateTree {
                     Id = m.Id,
                     Title = string.Concat(StrignRepat("&nbsp;&nbsp;",leve),split,m.Title),
                     Leve = leve,
                     Brief = m.Brief,
                     IsEffect = m.IsEffect,
                     Sort = m.Sort,
                     IsDelete = m.IsDelete,
                     ParentId = m.ParentId,
                     TypeId = m.TypeId
                 };
                 container.Add(tree);
                 if(m.HasChild) {
                    ToFormatTree(container,m.Child,leve + 1);
                 }
             }
        }
    }
}